import mixin from './opt';
import specialRule from './specialRule';
import utils from '@/mixins/utils.js';
export default {
  props: ['oldParkBaseInfo', 'oldPageChargeList', 'oldParkSpecialChargeVoList'],
  mixins: [mixin, specialRule, utils],
  data() {
    return {
      parkBaseInfo: this.oldParkBaseInfo,
      tabData: ['基本收费规则', '特殊收费规则'],
      currentTab: '基本收费规则',
      parkBaseInfoRules: {
        chargeRulePicStr: [{ required: true, message: '请上传收费规则图片', trigger: 'blur' }]
      },
      pageChargeList: {
        parkChargeBaseVoList: [
          {
            typeName: '计费组1',
            actionType: 0,
            carType: false,
            chargeTypeSeq: 0,
            isDefault: true,
            isFree: '收费车场',
            uiOpt: {
              isShow: true,
              carTypeOpt: [
                {
                  name: '蓝牌车',
                  value: 1,
                  disabled: false
                },
                {
                  name: '黄牌车',
                  value: 2,
                  disabled: false
                },
                {
                  name: '新能源小车',
                  value: 4,
                  disabled: false
                },
                {
                  name: '新能源大车',
                  value: 3,
                  disabled: false
                },
                {
                  name: '不区分',
                  value: 0,
                  disabled: false
                }
              ]
              // carTypeOpt: JSON.parse(JSON.stringify(this.opts.carTypeOpt))
            },
            parkChargeStandardVoList: [
              {
                uiOpt: {
                  splitTimeOpt: ['不分时段', '分时段'],
                  curSplitTime: '不分时段',
                  splitTimeCount: 2,
                  isMaxCharge: false,
                  customIsMaxCharge: false,
                  curTimeType: '分钟',
                  isShowRuleDetail: true
                },
                actionType: 0,
                carType: 0,
                chargeTypeSeq: 0,
                chargeUnit: '',
                cmxSettleType: 0,
                customMaxCharge: '',
                customMaxUnit: '',
                freeTime: '',
                freeTimeAcc: false,
                loopType: 1,
                maxCharge: '',
                minCharge: 0,
                mxSettleType: 0,
                natureDay: 0,
                natureFreeTime: 0,
                parkChargeStandardPeriodVoList: [
                  {
                    uiOpt: {
                      ruleTypeOpt: ['按次定额收费', '按单价收费', '递增收费'],
                      curRuleType: '按次定额收费'
                    },
                    maxCharge: 0,
                    mxSettleType: 0,
                    parkChargeStandardPeriodDetailVoList: [
                      {
                        chargeAmount: '',
                        chargeType: 3,
                        chargeUnit: 1,
                        periodDetailSeq: '',
                        standardPeriodSeq: '',
                        stepType: null
                      }
                    ],
                    standardPeriodSeq: '',
                    standardSeq: '',
                    timeArray: ['00:00:00', '23:59:59']
                  }
                ],
                remark: '',
                standardName: '默认规则',
                standardSeq: 0,
                startTime: '00:00:00',
                type: 0
              }
            ]
          }
        ]
      }
    };
  },
  created() {
    if (this.oldPageChargeList) {
      this.pageChargeList = this.oldPageChargeList;
    }
    if (this.oldParkSpecialChargeVoList) {
      this.parkSpecialChargeVoList = this.oldParkSpecialChargeVoList;
    }
  },
  methods: {
    changeTab(item) {
      this.currentTab = item;
      if (item === '特殊收费规则') {
        this.isAddingSpecialRule = false;
      }
    },
    setDefaultRuleGroup(val, itemIndex) {
      this.pageChargeList.parkChargeBaseVoList.forEach((item, index) => {
        if (index === itemIndex) {
          item.isDefault = val;
        } else {
          item.isDefault = false;
        }
      });
    },
    addRuleGroup() {
      this.pageChargeList.parkChargeBaseVoList.push({
        typeName: `计费组${this.pageChargeList.parkChargeBaseVoList.length + 1}`,
        actionType: 0,
        carType: false,
        chargeTypeSeq: 0,
        isDefault: false,
        isFree: '收费车场',
        uiOpt: {
          isShow: true,
          carTypeOpt: JSON.parse(JSON.stringify(this.opts.carTypeOpt))
        },
        parkChargeStandardVoList: [
          {
            uiOpt: {
              splitTimeOpt: ['不分时段', '分时段'],
              curSplitTime: '不分时段',
              splitTimeCount: 2,
              isMaxCharge: false,
              customIsMaxCharge: false,
              curTimeType: '分钟',
              isShowRuleDetail: true
            },
            actionType: 0,
            carType: '',
            chargeTypeSeq: 0,
            chargeUnit: 1,
            cmxSettleType: 0,
            customMaxCharge: '',
            customMaxUnit: '',
            freeTime: '',
            freeTimeAcc: true,
            loopType: 1,
            maxCharge: 0,
            minCharge: 0,
            mxSettleType: 0,
            natureDay: 0,
            natureFreeTime: 0,
            parkChargeStandardPeriodVoList: [
              {
                uiOpt: {
                  ruleTypeOpt: ['按次定额收费', '按单价收费', '递增收费'],
                  curRuleType: '按次定额收费'
                },
                maxCharge: 0,
                mxSettleType: 0,
                parkChargeStandardPeriodDetailVoList: [
                  {
                    chargeAmount: '',
                    chargeType: 3,
                    chargeUnit: 1,
                    periodDetailSeq: '',
                    standardPeriodSeq: '',
                    stepType: null
                  }
                ],
                standardPeriodSeq: '',
                standardSeq: '',
                timeArray: ['00:00:00', '23:59:59']
              }
            ],
            remark: '',
            standardName: '',
            standardSeq: 0,
            startTime: '00:00:00',
            type: 0
          }
        ]
      });
    },
    delRuleGroup(list, index, item) {
      if (list.length === 1) {
        this.showToast('至少保留一个计费组', 0, 2000);
        return;
      }
      if (item.isDefault) {
        this.showToast('默认计费组不允许删除', 0, 2000);
        return;
      }
      list.splice(index, 1);
    },
    addRuleItem(list, carTypeOpt) {
      if (list[0].carType === 0) {
        this.showToast('适用车型为"不区分"只需配置一种收费规则', 0, 2000);
        return;
      }

      list.push({
        uiOpt: {
          splitTimeOpt: ['不分时段', '分时段'],
          curSplitTime: '不分时段',
          splitTimeCount: 2,
          isMaxCharge: false,
          customIsMaxCharge: false,
          curTimeType: '分钟',
          isShowRuleDetail: true
        },
        actionType: 0,
        carType: '',
        chargeTypeSeq: 0,
        chargeUnit: 1,
        cmxSettleType: 0,
        customMaxCharge: '',
        customMaxUnit: '',
        freeTime: '',
        freeTimeAcc: true,
        loopType: 1,
        maxCharge: '',
        minCharge: 0,
        mxSettleType: 0,
        natureDay: 0,
        natureFreeTime: 0,
        parkChargeStandardPeriodVoList: [
          {
            uiOpt: {
              ruleTypeOpt: ['按次定额收费', '按单价收费', '递增收费'],
              curRuleType: '按次定额收费'
            },
            maxCharge: 0,
            mxSettleType: 0,
            parkChargeStandardPeriodDetailVoList: [
              {
                chargeAmount: '',
                chargeType: 3,
                chargeUnit: 1,
                periodDetailSeq: '',
                standardPeriodSeq: '',
                stepType: null
              }
            ],
            standardPeriodSeq: '',
            standardSeq: '',
            timeArray: ['00:00:00', '23:59:59']
          }
        ],
        remark: '',
        standardName: '',
        standardSeq: 0,
        startTime: '00:00:00',
        type: 0
      });

      this.setChargeRuleCarTypeDisabled(list, carTypeOpt);
    },
    changeChargeRuleType(val, parkChargeBaseVoListItem, carTypeOpt) {
      if (val === '免费车场') {
        parkChargeBaseVoListItem.carType = false;
        parkChargeBaseVoListItem.parkChargeStandardVoList = [
          {
            actionType: 0,
            carType: 0,
            chargeTypeSeq: 0,
            chargeUnit: '',
            cmxSettleType: 0,
            customMaxCharge: '',
            customMaxUnit: '',
            freeTime: '',
            freeTimeAcc: true,
            loopType: 1,
            maxCharge: '',
            minCharge: 0,
            mxSettleType: 0,
            natureDay: 0,
            natureFreeTime: 0,
            parkChargeStandardPeriodVoList: [],
            remark: '',
            standardName: '默认免费规则',
            standardSeq: 0,
            startTime: '00:00:00',
            type: 1
          }
        ];
      }

      if (val === '收费车场') {
        parkChargeBaseVoListItem.parkChargeStandardVoList = [
          {
            uiOpt: {
              splitTimeOpt: ['不分时段', '分时段'],
              curSplitTime: '不分时段',
              splitTimeCount: 2,
              isMaxCharge: false,
              customIsMaxCharge: false,
              curTimeType: '分钟',
              isShowRuleDetail: true
            },
            actionType: 0,
            carType: 0,
            chargeTypeSeq: 0,
            chargeUnit: 1,
            cmxSettleType: 0,
            customMaxCharge: '',
            customMaxUnit: '',
            freeTime: '',
            freeTimeAcc: true,
            loopType: 1,
            maxCharge: '',
            minCharge: 0,
            mxSettleType: 0,
            natureDay: 0,
            natureFreeTime: 0,
            parkChargeStandardPeriodVoList: [
              {
                uiOpt: {
                  ruleTypeOpt: ['按次定额收费', '按单价收费', '递增收费'],
                  curRuleType: '按次定额收费'
                },
                maxCharge: 0,
                mxSettleType: 0,
                parkChargeStandardPeriodDetailVoList: [
                  {
                    chargeAmount: '',
                    chargeType: 3,
                    chargeUnit: 1,
                    periodDetailSeq: '',
                    standardPeriodSeq: '',
                    stepType: null
                  }
                ],
                standardPeriodSeq: '',
                standardSeq: '',
                timeArray: ['00:00:00', '23:59:59']
              }
            ],
            remark: '',
            standardName: '',
            standardSeq: 0,
            startTime: '00:00:00',
            type: 0
          }
        ];

        this.setChargeRuleCarTypeDisabled(parkChargeBaseVoListItem.parkChargeStandardVoList, carTypeOpt);
      }
    },
    timeRuleMaxChargeChange(val, item) {
      if (!val) {
        item.maxCharge = 0;
      }
    },
    setChargeRuleIsTimes(val, parkChargeStandardVoListItem) {
      if (val === '不分时段') {
        parkChargeStandardVoListItem.mxSettleType = 0;
        parkChargeStandardVoListItem.cmxSettleType = 0;

        parkChargeStandardVoListItem.parkChargeStandardPeriodVoList = [
          {
            uiOpt: {
              ruleTypeOpt: ['按次定额收费', '按单价收费', '递增收费'],
              curRuleType: '按次定额收费'
            },
            maxCharge: 0,
            mxSettleType: 0,
            parkChargeStandardPeriodDetailVoList: [
              {
                chargeAmount: '',
                chargeType: 3,
                chargeUnit: 1,
                periodDetailSeq: '',
                standardPeriodSeq: '',
                stepType: null
              }
            ],
            standardPeriodSeq: '',
            standardSeq: '',
            timeArray: ['00:00:00', '23:59:59']
          }
        ];
      }

      if (val === '分时段') {
        parkChargeStandardVoListItem.mxSettleType = 1;
        parkChargeStandardVoListItem.cmxSettleType = 1;
        parkChargeStandardVoListItem.parkChargeStandardPeriodVoList = [
          {
            uiOpt: {
              ruleTypeOpt: ['按次定额收费', '按单价收费', '递增收费'],
              curRuleType: '按次定额收费',
              isMaxCharge: false
            },
            maxCharge: 0,
            mxSettleType: 0,
            parkChargeStandardPeriodDetailVoList: [
              {
                chargeAmount: '',
                chargeType: 3,
                chargeUnit: 1,
                periodDetailSeq: '',
                standardPeriodSeq: '',
                stepType: null
              }
            ],
            standardPeriodSeq: '',
            standardSeq: '',
            timeArray: ['00:00:00', '23:59:59']
          },
          {
            uiOpt: {
              ruleTypeOpt: ['按次定额收费', '按单价收费', '递增收费'],
              curRuleType: '按次定额收费',
              isMaxCharge: false
            },
            maxCharge: 0,
            mxSettleType: 0,
            parkChargeStandardPeriodDetailVoList: [
              {
                chargeAmount: '',
                chargeType: 3,
                chargeUnit: 1,
                periodDetailSeq: '',
                standardPeriodSeq: '',
                stepType: null
              }
            ],
            standardPeriodSeq: '',
            standardSeq: '',
            timeArray: ['00:00:00', '23:59:59']
          }
        ];
      }
    },
    timeRuleChange(val, item) {
      if (val === '按次定额收费') {
        item.uiOpt.isMaxCharge = false;
        item.maxCharge = 0;
        item.parkChargeStandardPeriodDetailVoList = [
          {
            chargeAmount: '',
            chargeType: 3,
            chargeUnit: 1,
            periodDetailSeq: '',
            standardPeriodSeq: '',
            stepType: null
          }
        ];
      }
      if (val === '按单价收费') {
        item.parkChargeStandardPeriodDetailVoList = [
          {
            uiOpt: {
              curTimeType: '分钟'
            },
            chargeAmount: '',
            chargeType: 1,
            chargeUnit: '',
            periodDetailSeq: '',
            standardPeriodSeq: '',
            stepType: ''
          }
        ];
      }
      if (val === '递增收费') {
        item.isHasXunhuan = true;
        item.parkChargeStandardPeriodDetailVoList = [
          {
            uiOpt: {
              curTimeType: '分钟'
            },
            chargeAmount: '',
            chargeType: 2,
            chargeUnit: '',
            periodDetailSeq: '',
            standardPeriodSeq: '',
            stepType: 1
          },
          {
            uiOpt: {
              curTimeType: '分钟'
            },
            chargeAmount: '',
            chargeType: 2,
            chargeUnit: '',
            periodDetailSeq: '',
            standardPeriodSeq: '',
            stepType: 2
          }
        ];
      }
    },
    addDingE_RuleItem(detailList) {
      detailList.push({
        uiOpt: {
          curTimeType: '分钟'
        },
        chargeAmount: '',
        chargeType: 2,
        chargeUnit: '',
        periodDetailSeq: '',
        standardPeriodSeq: '',
        stepType: 1
      });
    },
    addXunHuan_RuleItem(ruleItem, detailList) {
      ruleItem.isHasXunhuan = true;
      detailList.push({
        uiOpt: {
          curTimeType: '分钟'
        },
        chargeAmount: '',
        chargeType: 2,
        chargeUnit: '',
        periodDetailSeq: '',
        standardPeriodSeq: '',
        stepType: 2
      });
    },
    delRuleItemDetailItem(list, index) {
      list.splice(index, 1);
    },
    delParkChargeStandardPeriodVoListItem(item, list, index) {
      item.uiOpt.splitTimeCount--;
      list.splice(index, 1);
    },
    delRuleItemDetailXunHuanItem(ruleItem, list, index) {
      ruleItem.isHasXunhuan = false;
      list.splice(index, 1);
    },
    splitTimeChange(val, list) {
      if (val > list.length) {
        for (let i = 0, len = val - list.length; i < len; i++) {
          list.push({
            uiOpt: {
              ruleTypeOpt: ['按次定额收费', '按单价收费', '递增收费'],
              curRuleType: '按次定额收费'
            },
            maxCharge: 0,
            isMaxCharge: false,
            mxSettleType: 0,
            parkChargeStandardPeriodDetailVoList: [
              {
                chargeAmount: '',
                chargeType: 3,
                chargeUnit: 1,
                periodDetailSeq: '',
                standardPeriodSeq: '',
                stepType: null
              }
            ],
            standardPeriodSeq: '',
            standardSeq: '',
            timeArray: ['00:00:00', '23:59:59']
          });
        }
      }
      if (list.length > val) {
        list.splice(-(list.length - val), list.length - val);
      }
    },
    customIsMaxChargeChange(val, item) {
      if (!val) {
        item.customMaxUnit = '';
        item.customMaxCharge = '';
      }
    },
    isMaxChargeChange(val, item) {
      if (!val) {
        item.maxCharge = '';
      }
    },
    setChargeRuleCarTypeDisabled(list, carTypeOpt) {
      const selectedCarTypes = list.map(item => {
        return item.carType;
      });
      const selectedCarTypes_length = selectedCarTypes.length;
      carTypeOpt.forEach(sitem => {
        if (selectedCarTypes.indexOf(sitem.value) !== -1 && sitem.value !== 0) {
          sitem.disabled = true;
        } else if (sitem.value === 0 && (selectedCarTypes_length >= 2 || list.length >= 2)) {
          sitem.disabled = true;
        } else {
          sitem.disabled = false;
        }
      });
    },
    delRules(list, index, carTypeOpt) {
      list.splice(index, 1);
      this.setChargeRuleCarTypeDisabled(list, carTypeOpt);
    },
    async handleChange(file, fileList) {
      const isJPG = file.raw.type === 'image/jpeg' || file.raw.type === 'image/png';
      const isLt900K = file.raw.size / 1024 / 1024 < 0.87;
      if (!isJPG) {
        console.log('上传图片只能是 JPG 格式或者 PNG 格式!');
      }
      if (!isLt900K) {
        console.log('上传图片大小不能超过 900K!');
      }
      console.log(fileList);
      if (isJPG && isLt900K) {
        this.parkBaseInfo.uiOpt.chargeRuleImgList = fileList.slice(-1);
        const imgData = await this.getUploadImgData(file);
        this.parkBaseInfo.uiOpt.chargeRuleImgList[0].url = imgData.base64Str;
        this.parkBaseInfo.chargeRulePicStr = imgData.base64Str.split(',')[1];
        this.parkBaseInfo.chargeRulePicType = imgData.imgType;
        this.testParkChargeRulelImg();
      } else {
        this.parkBaseInfo.uiOpt.chargeRuleImgList = [];
        this.parkBaseInfo.chargeRulePicStr = '';
        this.parkBaseInfo.chargeRulePicType = '';
      }
    },
    getUploadImgData(file) {
      return new Promise((resolve, reject) => {
        const reader = new FileReader();
        const self = this;
        reader.readAsDataURL(file.raw);
        reader.onload = e => {
          const result = {
            base64Str: e.target.result,
            imgType: file.raw.type.split('/')[1]
          };
          resolve(result);
        };
      });
    },
    downloadRuleTemplate() {
      API.fetchGet('/operatorPark/getOperatorParkChargeTemplateUrl').then(res => {
        window.open(res.data);
      });
    },
    goBackStep() {
      this.$emit('setFormData', 'parkBaseInfo', this.parkBaseInfo);
      this.$emit('setFormData', 'pageChargeList', this.pageChargeList);
      this.$emit('setFormData', 'parkSpecialChargeVoList', this.parkSpecialChargeVoList);
      this.$emit('goBackSetp');
    },
    testXunHuanRule() {
      const self = this;
      // 递增收费如果全部是定额，时段内的定额分钟数的最大值必须等于时段分钟数，否则必须加上循环
      let result = [];
      this.pageChargeList.parkChargeBaseVoList.forEach(item => {
        item.parkChargeStandardVoList.forEach(citem => {
          // 不分时段递增收费
          if (
            citem.parkChargeStandardPeriodVoList.length === 1 &&
            citem.parkChargeStandardPeriodVoList[0].uiOpt.curRuleType === '递增收费'
          ) {
            const has_xun_huan_rule = citem.parkChargeStandardPeriodVoList[0].parkChargeStandardPeriodDetailVoList.filter(
              item => item.chargeType === 2 && item.stepType === 2
            );
            if (!has_xun_huan_rule.length) {
              let max_chargeUnit = 0;
              citem.parkChargeStandardPeriodVoList[0].parkChargeStandardPeriodDetailVoList.forEach(ccitem => {
                if (ccitem.chargeUnit > max_chargeUnit) {
                  max_chargeUnit = ccitem.chargeUnit;
                }
              });
              if (max_chargeUnit !== 1440) {
                let carType = this.opts.carTypeOpt.filter(item => item.value === citem.carType)[0].name;
                result.push(`${item.typeName},${carType}`);
              }
            }
          }
          // 分时段
          if (citem.parkChargeStandardPeriodVoList.length > 1) {
            citem.parkChargeStandardPeriodVoList.forEach((pitem, index) => {
              if (pitem.uiOpt.curRuleType === '递增收费') {
                const has_xun_huan_rule = pitem.parkChargeStandardPeriodDetailVoList.filter(
                  item => item.chargeType === 2 && item.stepType === 2
                );

                if (!has_xun_huan_rule.length) {
                  let max_chargeUnit = 0;
                  citem.parkChargeStandardPeriodVoList[0].parkChargeStandardPeriodDetailVoList.forEach(ccitem => {
                    if (ccitem.chargeUnit > max_chargeUnit) {
                      max_chargeUnit = ccitem.chargeUnit;
                    }
                  });
                  const time_range_minutes = self.getRangTimeMinutes(pitem.timeArray);
                  if (time_range_minutes.residue !== 0 || max_chargeUnit !== time_range_minutes.minutes) {
                    let carType = this.opts.carTypeOpt.filter(item => item.value === citem.carType)[0].name;
                    result.push(`${item.typeName},${carType}时段${index + 1}`);
                  }
                }
              }
            });
          }
        });
      });
      return result;
    },
    getRangTimeMinutes(timeArray) {
      const start_time = this.getRuleRangTime(timeArray[0]);
      const end_time = this.getRuleRangTime(timeArray[1], 'end');
      let rang;
      const day_rang = 24 * 60 * 60 * 1000;
      if (start_time >= end_time) {
        rang = end_time + day_rang - start_time;
      } else {
        rang = end_time - start_time;
      }
      return {
        residue: rang % (60 * 1000),
        minutes: rang / (60 * 1000)
      };
    },
    getRuleRangTime(time, type) {
      const d = new Date();
      if (
        type === 'end' &&
        new Date([d.getFullYear(), d.getMonth() + 1, d.getDate()].join('/') + ' ' + time).getMinutes() === 59
      ) {
        return new Date([d.getFullYear(), d.getMonth() + 1, d.getDate()].join('/') + ' ' + time).getTime() + 1000;
      } else {
        return new Date([d.getFullYear(), d.getMonth() + 1, d.getDate()].join('/') + ' ' + time).getTime();
      }
    },
    async goNextStep(formName) {
      const testCarTypes = this.testCarTypes();
      if (!testCarTypes) return;
      const testRuleImg = await this.testParkChargeRulelImg();
      const testRule = await this.testRule();
      const testSpecialListRule = await this.testSpecialListRule();

      if (testCarTypes && testRuleImg && testRule && testSpecialListRule) {
        const isTestXunHuanRule = this.testXunHuanRule();
        if (isTestXunHuanRule.length) {
          let tips_string_html = isTestXunHuanRule.map(item => {
            const timeRange = item.includes('时段') ? '分时段' : '不分时段';
            if (timeRange === '不分时段') {
              return `<p>${item}收费规则配置错误</p><p style="margin-bottom: 10px">提示：不分时段递增收费规则，必须有一条规则的分钟数等于1440</p>`;
            } else if (timeRange === '分时段') {
              return `<p>${item}收费规则配置错误</p><p style="margin-bottom: 10px">提示：分时段递增收费规则，配置的分钟数必须等于时段内分钟数</p>`;
            }
          });
          this.$alert(tips_string_html.join(''), '收费规则配置错误', {
            confirmButtonText: '确定',
            customClass: 'mgr-msg-box rule-tips',
            confirmButtonClass: 'el-button el-button--danger',
            dangerouslyUseHTMLString: true
          });
          return;
        }
        this.$emit('setFormData', 'parkBaseInfo', this.parkBaseInfo);
        this.$emit('setFormData', 'pageChargeList', this.pageChargeList);
        this.$emit('setFormData', 'parkSpecialChargeVoList', this.parkSpecialChargeVoList);
        this.$emit('goNextStep');
      }
    },

    testCarTypes() {
      //如果是收费类型，适用车型不是不区分，则必须同时配置蓝牌车、黄牌车、新能源小车、新能源大车的收费规则
      let flag = true;
      const list = this.pageChargeList.parkChargeBaseVoList.filter(item => item.isFree === '收费车场');
      if (!list.length) return true;

      list.some((item, index) => {
        const hasTypes = item.parkChargeStandardVoList.map(item => {
          return item.carType;
        });
        if (
          hasTypes.includes(0) &&
          (hasTypes.includes(2) || hasTypes.includes(1) || hasTypes.includes(3) || hasTypes.includes(4))
        ) {
          this.showToast(`收费组${index + 1}适用车型是不区分只需要配置一个规则`, 0, 4000);
          flag = false;
          return true;
        }
        if (
          !hasTypes.includes(0) &&
          !(hasTypes.includes(1) && hasTypes.includes(2) && hasTypes.includes(3) && hasTypes.includes(4))
        ) {
          this.showToast(`收费组${index + 1}必须同时配置蓝牌车、黄牌车、新能源小车、新能源大车的收费规则`, 0, 4000);
          flag = false;
          return true;
        }
      });

      return flag;
    },

    testParkChargeRulelImg() {
      return new Promise((resolve, reject) => {
        this.$refs['parkBaseInfo'].validate(valid => {
          if (valid) {
            resolve(true);
          } else {
            resolve(false);
          }
        });
      });
    },

    testRule() {
      const self = this;
      let test = [];
      return new Promise((resolve, reject) => {
        const testRuleItem = testItemIndex => {
          let result;
          this.$refs['pageChargeList'][testItemIndex].validate(valid => {
            if (valid) {
              result = true;
            } else {
              result = false;
            }
          });
          return result;
        };
        this.pageChargeList.parkChargeBaseVoList.forEach((item, index) => {
          test.push(testRuleItem(index));
        });

        if (!test.includes(false)) {
          resolve(true);
        } else {
          resolve(false);
        }
      });
    },

    testSpecialListRule() {
      return new Promise((resolve, reject) => {
        console.log(this.$refs['parkSpecialChargeVoList']);
        this.$refs['parkSpecialChargeVoList'].validate(valid => {
          if (valid) {
            resolve(true);
          } else {
            resolve(false);
          }
        });
      });
    },

    checkUnit(rule, value, callback) {
      const integer_reg = /^[1-9]\d*$/
      if (value === '') {
        callback(new Error('时间不能为空'));
      } else if (!integer_reg.test(value)) {
        callback(new Error('必须是大于0的整数'));
      } else {
        callback();
      }
    },
    checkMoney(rule, value, callback) {
      if (value === '') {
        callback(new Error('金额不能为空'));
      } else {
        const v = Number(value)
        if (!(v >= 0)) {
          callback(new Error('必须大于等于0'));
        } else {
          callback();
        }
      }
    },

    beforeAvatarUpload(file) {
      return false;
    }
  }
};
